﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Collections;
using System.Reflection;
using UMC.Web;
using UMC.Data.Entities;
using UMC.Web.UI;
using UMC.ITME.Entities;

namespace UMC.ITME.Activities
{
    /// <summary>
    /// 邮箱账户
    /// </summary>
    [UMC.Web.Apiumc("ITME", "Auth", Auth = WebAuthType.Guest)]
    public class SiteAuthActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var seesionKey = Utility.MD5(this.Context.Token.Device.Value);

            var sesion = UMC.Data.DataFactory.Instance().Session(this.Context.Token.Device.ToString());

            if (sesion != null)
            {
                sesion.SessionKey = seesionKey;
                UMC.Data.DataFactory.Instance().Put(sesion);

                var oauth_callback = this.AsyncDialog("oauth_callback", "none");
                if (string.Equals("none", oauth_callback))
                {

                    response.Redirect(new WebMeta().Put("AuthKey", seesionKey));
                }
                else
                {

                    var UI = this.AsyncDialog("UI", "none");
                    var url = new Uri(Uri.UnescapeDataString(oauth_callback));
                    if (String.IsNullOrEmpty(url.Query))
                    {
                        this.Context.Send(new WebMeta().UIEvent("UI.Browser", UI, new WebMeta("value", new Uri(url, $"/!/{seesionKey}{url.PathAndQuery}?_t={Utility.TimeSpan()}").AbsoluteUri)), true);//, new WebMeta().Put("value", oauth_callback), true);
                    }
                    else
                    {
                        this.Context.Send(new WebMeta().UIEvent("UI.Browser", UI, new WebMeta("value", new Uri(url, $"/!/{seesionKey}{url.PathAndQuery}&_t={Utility.TimeSpan()}").AbsoluteUri)), true);//, new WebMeta().Put("value", oauth_callback), true);

                    }
                }

            }

        }
    }
}